package com.situ.crm.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.crm.common.ServerResponse;
import com.situ.crm.entity.Order;
import com.situ.crm.entity.Permission;
import com.situ.crm.entity.Role;
import com.situ.crm.entity.RolePermission;
import com.situ.crm.mapper.PermissionMapper;
import com.situ.crm.mapper.RoleMapper;
import com.situ.crm.mapper.RolePermissionMapper;
import com.situ.crm.service.IRoleService;
import com.situ.crm.vo.LayUISelectMVO;
@Service
public class RoleServiceImpl implements IRoleService {
	@Autowired
	private RoleMapper roleMapper;
	@Autowired
	private RolePermissionMapper rolePermissionMapper;
	@Autowired
	private PermissionMapper permissionMapper;
	 
	@Override
	public ServerResponse pageList(Integer limit, Integer page, Role role) {
		//开启分页
		PageHelper.startPage(page,limit);
		//查信息
		List<Role> list = roleMapper.pageList(role);
		//封装pageInfo
		PageInfo pageInfo = new PageInfo<>(list);
		//总数据
		Integer count = (int)pageInfo.getTotal();
		
		return ServerResponse.createSuccess("查找成功", count, list);
	}

	//添加、添加权限到role_permission表
	@Override
	public ServerResponse insert(Role role, String permissions) {
		try {
			Integer count = roleMapper.insert(role);
			//把字符串转换为字符数组，插入到role_permission表
			String[] permissionIds = permissions.split(",");
			for (String permissionId : permissionIds) {
				//这里要拿到插入后的roleId，插入数据库后可以直接得到自动生成的id。要在mapper.xml加useGeneratedKeys="true" keyProperty="id"
				RolePermission rolePermission = new RolePermission(role.getId(),Integer.parseInt(permissionId));
				rolePermissionMapper.insert(rolePermission);
			}
			return ServerResponse.createSuccess("添加成功");
		} catch (Exception e) {
			return ServerResponse.createSuccess("添加失败");
		}
	}

	//根据Id删除
	@Override
	public ServerResponse deleteById(Integer id) {
		try {
			rolePermissionMapper.deleteByroleId(id);
			Integer count = roleMapper.deleteByPrimaryKey(id);
			if (count == 1) {
				return ServerResponse.createSuccess("删除成功");
			} else {
				return ServerResponse.createError("删除失败");
			}
		} catch (Exception e) {
			return ServerResponse.createError("删除失败");
		}
	}

	//批量删除
	@Override
	public ServerResponse deleteAll(String ids) {
		String[] idArrays = ids.split(",");
		try {
			rolePermissionMapper.deleteAllByRoleId(idArrays);
			Integer count = roleMapper.deleteAll(idArrays);
			if (count >= 1) {
				return ServerResponse.createSuccess("删除成功");
			} else {
				return ServerResponse.createError("删除失败");
			}
		} catch (Exception e) {
			return ServerResponse.createError("删除失败");
		}
	}

		//roleMapper.selectByPrimaryKey(id);

	//修改页面的查询
	@Override
	public ServerResponse selectRoleAndPermission(Integer roleId) {
		//建map
		Map<String, Object> map = new HashMap<>();
		//把Role放进去
		Role role = roleMapper.selectByPrimaryKey(roleId);
		map.put("role", role);
		
		//查询所有的权限
		List<Permission> permissions = permissionMapper.pageList(new Permission());
		//把所有权限封装成 LayUISelectMVO
		List<LayUISelectMVO> list = new ArrayList<>();
		for (Permission permission : permissions) {
			LayUISelectMVO layUISelectMVO = new LayUISelectMVO();
			layUISelectMVO.setId(permission.getId());
			layUISelectMVO.setName(permission.getName());
			layUISelectMVO.setStatus(1);
			list.add(layUISelectMVO);
		}
		//把封装后的权限放进去
		map.put("permissions", list);
		
		//查询角色Id对应的permission,即role_permission表，数据就是数组
		List<Integer> roleSelectPermission = rolePermissionMapper.roleSelectPermission(roleId);
		//集合转数组.toArray()
		map.put("selectIds", roleSelectPermission.toArray());
		return ServerResponse.createSuccess("查找成功",map);
	}

	//修改 功能
	@Override
	public ServerResponse update(Role role, String permissions) {
		try {
			Integer count = roleMapper.updateByPrimaryKeySelective(role);
			//删除原来的权限，再插入，实现更新
			rolePermissionMapper.deleteByroleId(role.getId());
			String[] permissionIds = permissions.split(",");
			for (String permissionId : permissionIds) {
				RolePermission rolePermission = new RolePermission(role.getId(),Integer.parseInt(permissionId));
				rolePermissionMapper.insert(rolePermission);
			}
			return ServerResponse.createSuccess("修改成功");
		} catch (Exception e) {
			return ServerResponse.createSuccess("修改失败");
		}
		
	}

	//用户添加页面查询所有的角色
	@Override
	public ServerResponse selectAllRole() {
		//发送个空的，相当于查询所有
		List<Role> roles = roleMapper.pageList(new Role());
		
		List<LayUISelectMVO> list = new ArrayList<>();
		for (Role role : roles) {
			LayUISelectMVO layUISelectMVO = new LayUISelectMVO();
			layUISelectMVO.setId(role.getId());
			layUISelectMVO.setName(role.getName());
			layUISelectMVO.setStatus(1);
			list.add(layUISelectMVO);
		}
		return ServerResponse.createSuccess("权限查找成功ヾ(≧▽≦*)o", list);
	}
}
